package midalgorithms.arrandstr;

public class _02SetZeroes {
    public static void setZeroes(int[][] matrix) {
        //  *后期可以了解一下：原地算法
        int rowLen = matrix.length,colLen = matrix[0].length;
        //  如果直接做置零操作的话，那么后来置的0会干扰后续的置零
        //  两个临时数组可以记录行和列是否存在0
        boolean[] ifRow = new boolean[rowLen];
        boolean[] ifCol = new boolean[colLen];
        //  遍历取0
        for (int i = 0;i < rowLen;i++){
            for (int j = 0;j < colLen;j++){
                if (matrix[i][j] == 0){
                    ifRow[i] = true;
                    ifCol[j] = true;
                }
            }
        }
        //  置零
        for (int i = 0;i < rowLen;i++){
            for (int j = 0;j < colLen;j++){
                if (ifRow[i] || ifCol[j]){
                    matrix[i][j] = 0;
                }
            }
        }
    }
}
